package com.parkingspace.controller;


import com.parkingspace.entity.ParkingInformation;
import com.parkingspace.entity.Property;

import com.parkingspace.entity.PropertyBetweenUser;
import com.parkingspace.entity.User;
import com.parkingspace.service.impl.ParkingInformationServiceImpl;
import com.parkingspace.service.impl.PropertyBetweenUserServiceImpl;
import com.parkingspace.service.impl.PropertyServiceImpl;
import com.parkingspace.service.impl.UserServiceImpl;
import com.parkingspace.util.Constants;
import com.parkingspace.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 物业公司与用户表的中间表 前端控制器
 * </p>
 *
 * @author Gapper
 * @since 2021-05-24
 */
@RestController
@RequestMapping("/PropertyBetweenUser")
public class PropertyBetweenUserController {

    @Resource
    private  PropertyBetweenUserServiceImpl propertyBetweenUserService;

    @Resource
    private  UserServiceImpl userService;

    @Resource
    private ParkingInformationServiceImpl parkingInformationService;

    @Resource
    private PropertyServiceImpl propertyService;

    @GetMapping ("/getPropertyBetweenUserList")
    public ResultVO  getUsersByPid(String token,PropertyBetweenUser propertyBetweenUser) {
        System.out.println("PropertyBetweenUserController进来了"+propertyBetweenUser.getPbuPropertyId());
        System.out.println("==============================");
        ArrayList<Object> list = new ArrayList<>();

        if (token==null){
            return null;
        }

        //根据pid获取到物业的信息
        Property property = propertyService.getPropertyById(propertyBetweenUser.getPbuPropertyId());
        list.add(property);

        //根据物业id查到车位信息
        ParkingInformation parking = parkingInformationService.getParkingByPid(propertyBetweenUser.getPbuPropertyId()).get(0);
        list.add(parking);

        //根据pid获取到PropertyBetweenUser的用户id集合
//        ArrayList<Long> ids = new ArrayList<>();
//        List<PropertyBetweenUser> propertyBetweenUserList = propertyBetweenUserService.getPropertyBetweenUserList(propertyBetweenUser.getPbuPropertyId());
//        for (PropertyBetweenUser betweenUser : propertyBetweenUserList) {
//            ids.add(betweenUser.getPbuUserId());
//        }
//        //根据用户id，查到用户集合
//        ArrayList<User> users = new ArrayList<>();
//        for (Long id : ids) {
//            User user = userService.getUserById(id);
//            users.add(user);
//        }
//        System.out.println("PropertyBetweenUserController得到的数据："+users);
//        //根据用户id查到车位id集合
//        List<ParkingInformation> parkingInformations = null;
//        System.out.println("车位前");
//        for (Long id : ids) {
//
//            parkingInformations = parkingInformationService.getParkingByUid(id);
//
//        }
//        System.out.println("parkingInformations车位 = " + parkingInformations);
//        list.add(parkingInformations.get(0));

        ResultVO resultVO;
        if (list.size() == 0){
            resultVO = new ResultVO(Constants.OPER_FAILURE,"查询失败",list);
            return resultVO;
        }
        resultVO = new ResultVO(Constants.OPER_SUCCESS,"成功",list);
        return resultVO;
    }



}

